//
//  LKAlianceLeaderApplyViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/28.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKAlianceLeaderApplyViewController.h"
#import "LKUFPictureRow.h"
#import "LKUFPictureRowCell.h"
#import "LKUFOpenShopHeaderRow.h"
#import "LKUFOpenShopHeaderRowCell.h"
#import "LKUFOpenShopBottomButtonRow.h"
#import "LKUFOpenShopBottomButtonRowCell.h"
@interface LKAlianceLeaderApplyViewController ()
@property(nonatomic,strong)UFFormView *formview;
@property(nonatomic,assign)NSInteger pictureTapSelectIndex;
@property(nonatomic,strong)UIImage *selcetedUploadImage;
@end

@implementation LKAlianceLeaderApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"成为盟主";
    [self createFormView];
    // Do any additional setup after loading the view.
}
-(void)createFormView{
    WS(weakSelf)
    self.formview = [UFFormView makeFormView:^(UFFormViewMaker * _Nonnull make) {
        make
        .registerRow([LKUFOpenShopHeaderRow class], [LKUFOpenShopHeaderRowCell class])
        .registerRow([LKUFPictureRow class], [LKUFPictureRowCell class])
        .registerRow([LKUFOpenShopBottomButtonRow class], [LKUFOpenShopBottomButtonRowCell class])
        .addSection([UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
            make
            .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
               make
                .width(WIDTH/2)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor)
                .textAlignment(NSTextAlignmentLeft);
            }])
            .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
                make
                .textAlignment(NSTextAlignmentRight)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .value(@"")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                })
                .title(@"姓名");
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .value(@"")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                })
                .title(@"省份证号码");
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .value(@"")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                })
                .title(@"手机号码");
            }])
            .addRow([UFAreaPickerRow makeAreaPickerRow:^(UFAreaPickerRowMaker * _Nonnull make) {
                make
                .areaSeparator(@",")
                .height(RowHeightDefault)
                .title(@"所在地址")
                .value(@"")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
//                    NSArray *addressArray = [value componentsSeparatedByString:@","];
//                    if (addressArray.count == 3) {
//                        weakSelf.inforModel.province = addressArray[0];
//                        weakSelf.inforModel.city = addressArray[1];
//                        weakSelf.inforModel.county = addressArray[2];
//                    }else{
//                        weakSelf.inforModel.province = addressArray[0];
//                        weakSelf.inforModel.county = addressArray[1];
//                        if ([weakSelf.inforModel.province containsString:@"市"]) {
//                            weakSelf.inforModel.county = addressArray[1];
//                            weakSelf.inforModel.city = [weakSelf.inforModel.province mutableCopy];
//                            weakSelf.inforModel.province = [[weakSelf.inforModel.province componentsSeparatedByString:@"市"] firstObject];
//                        }
//                    }
                })
                .accessoryType(UFRowAccessoryDisclosureIndicator);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .value(@"")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {

                })
                .title(@"详细地址");
            }])
            .addRowWithBlock(^__kindof UFRow * _Nonnull{
                LKUFPictureRow *pictureRow = [[LKUFPictureRow alloc] init];
                pictureRow.headerTitle = @"请上传身份证件";
                pictureRow.pictureNumbers = 2;
                pictureRow.titlesPlaceArray = @[@"+身份证正面照",@"+身份证背面照"];
                pictureRow.height = 145;
                pictureRow.uploadImageBlock = ^(NSInteger selectValue, UIImageView * _Nonnull selecteImageV) {
                    weakSelf.pictureTapSelectIndex = 4;
                    [weakSelf showAlertChooseVc];
                };
                return pictureRow;
            });
        }])
        .addToSuperView(self.view);
    }];
    [self.formview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT);
        make.bottom.equalTo(self.view);
    }];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    WS(weakSelf)
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {

        UIImage *photo = [info objectForKey:UIImagePickerControllerOriginalImage];
        LKUFPictureRow *row = [self.formview findRowInRange:UFMakeRange(0, self.pictureTapSelectIndex)];
        NSLog(@"选中的%ld",row.selectImageV.tag);
        row.selectImageV.superview.userInteractionEnabled = NO;
        row.selectImageV.image = photo;
            self.selcetedUploadImage = row.selectImageV.image;
            [SVProgressHUD showWithStatus:@"图片上传中..."];
        [[LKNetDataManager defaultManager] sendPOSTRequestWithserverUrl:ServerlUrl apiPath:ImageUploadUrl parameters:nil imageArray:@[self.selcetedUploadImage] targetWidth:100 progress:^(NSProgress * _Nullable progress) {
            
                } success:^(BOOL isSuccess, id  _Nullable responseObject) {
                 row.selectImageV.superview.userInteractionEnabled = YES;
                    [SVProgressHUD dismiss];
                    if ([responseObject[@"code"] isEqualToString:@"success"]) {
                        if ([NSString isBlankString:responseObject[@"data"]]) {
                            [SVProgressHUD showInfoWithStatus:@"图片未上传成功，请重新选择"];
                            return ;
                        }

                    }else{
                       [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                    }
            
                } failure:^(NSString * _Nullable error) {
            
                }];

        
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos{
    WS(weakSelf)
    LKUFPictureRow *row = [self.formview findRowInRange:UFMakeRange(0, self.pictureTapSelectIndex)];
    row.selectImageV.superview.userInteractionEnabled = NO;
    NSLog(@"选中的%ld",row.selectImageV.tag);
    row.selectImageV.image = photos[0];
    self.selcetedUploadImage = row.selectImageV.image;
    [SVProgressHUD showWithStatus:@"图片上传中..."];
    [[LKNetDataManager defaultManager] sendPOSTRequestWithserverUrl:ServerlUrl apiPath:ImageUploadUrl parameters:nil imageArray:@[self.selcetedUploadImage] targetWidth:100 progress:^(NSProgress * _Nullable progress) {
        
            } success:^(BOOL isSuccess, id  _Nullable responseObject) {
             row.selectImageV.superview.userInteractionEnabled = YES;
                [SVProgressHUD dismiss];
                if ([responseObject[@"code"] isEqualToString:@"success"]) {
                    if ([NSString isBlankString:responseObject[@"data"]]) {
                        [SVProgressHUD showInfoWithStatus:@"图片未上传成功，请重新选择"];
                        return ;
                    }

                }else{
                   [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                }
        
            } failure:^(NSString * _Nullable error) {
        
            }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
